package com.circuit.euler;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

/**
 * 
 * @author Monsif Mabrouk
 *
 */
public class LoadFile extends JFrame {

	private static final long serialVersionUID = -8476972735105617715L;
	private String fichier = "";
	Thread t;
	String filelist;
	JProgressBar pb;
	JLabel tExec = new JLabel("Temp d'execution : ");
	JButton openButton = new JButton("Ouvrir");
	JButton runButton = new JButton("Executer");
	JButton runButton2 = new JButton("Visualiser");
	JButton stop = new JButton("Stop");
	Container c = getContentPane();
	JLabel statusbar = new JLabel();
	JLabel choix= new JLabel("Sommet de depart : ");
	JTextField s_dep=new JTextField();
	int sommet_dep;
	public LoadFile() {
		super("MNT selector");
		setSize(450, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		
		c.setLayout(null);
		stop.setVisible(false);

		statusbar.setFont(new Font("Serif", Font.BOLD, 15));
		choix.setFont(new Font("Serif", Font.BOLD, 15));
		choix.setBounds(50, 70, 400, 20);
		s_dep.setBounds(200, 70, 30,20);
		tExec.setBounds(50, 180, 300, 50);
		tExec.setFont(new Font("Serif", Font.BOLD, 15));
		openButton.setBounds(40, 10, 80, 20);
		stop.setBounds(150, 10, 120, 20);
		stop.setBackground(Color.red);
		runButton.setBounds(300, 10, 100, 20);
		runButton2.setBounds(300, 30, 100, 20);
		statusbar.setBounds(50, 50, 400, 20);
		pb = new JProgressBar();
		pb.setValue(0);
		pb.setStringPainted(true);
		pb.setVisible(false);
		JPanel panel = new JPanel();
		panel.add(pb);
		JPanel panel1 = new JPanel();
		panel1.setLayout(new BorderLayout());
		panel1.add(panel, BorderLayout.NORTH);
		panel1.setBounds(50, 100, 300, 50);
		// Create a file chooser that opens up as an Open dialog
		openButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				fichier = "";
				JFileChooser chooser = new JFileChooser("./");
				chooser.setMultiSelectionEnabled(true);
				int option = chooser.showOpenDialog(LoadFile.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					File[] sf = chooser.getSelectedFiles();
					if (sf.length > 0)
						setFichier(sf[0].getPath());
					filelist = sf[0].getName();
					statusbar.setForeground(Color.black);
					statusbar.setText("You chose " + filelist);

				} else {
					statusbar.setForeground(Color.red);
					statusbar.setText("You canceled.");
				}
			}
		});
		// run the program if file is selected
		
		runButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if (fichier.length() == 0 ) {
					statusbar.setForeground(Color.red);
					statusbar
							.setText("program can't run please chose a file before !!");
				} else {
					if(s_dep.getText().length()==0){
						statusbar.setForeground(Color.red);
						statusbar
								.setText("veuillez choisir un sommet de depart !!");
					}else{
						try{
							sommet_dep=Integer.parseInt(s_dep.getText());
							statusbar.setForeground(Color.blue);
							statusbar.setText("program is running with " + filelist);
							openButton.setVisible(false);
							runButton.setVisible(false);
							runButton2.setVisible(false);
							stop.setVisible(true);
							t = new Thread() {
								public void run() {
								Execute.executer(getFichier(),LoadFile.this);
//								
								}
							};
							t.start();
						}catch(NumberFormatException e){
							statusbar.setForeground(Color.red);
							statusbar
									.setText("Veillez saisir un nombre entier");
						}
				
					}
				}

			}
		});
	
		runButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if (fichier.length() == 0 ) {
					statusbar.setForeground(Color.red);
					statusbar
							.setText("program can't run please chose a file before !!");
				} else {
					if(s_dep.getText().length()==0){
						statusbar.setForeground(Color.red);
						statusbar
								.setText("veuillez choisir un sommet de depart !!");
					}else{
						try{
							sommet_dep=Integer.parseInt(s_dep.getText());
							statusbar.setForeground(Color.blue);
							statusbar.setText("program is running with " + filelist);
							openButton.setVisible(false);
							runButton.setVisible(false);
							runButton2.setVisible(false);
							stop.setVisible(true);
							t = new Thread() {
								public void run() {
								Execute.executer2(getFichier(),LoadFile.this);
//								
								}
							};
							t.start();
						}catch(NumberFormatException e){
							statusbar.setForeground(Color.red);
							statusbar
									.setText("Veillez saisir un nombre entier");
						}
				
					}
				}

			}
		});


		stop.addActionListener(new ActionListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent arg0) {
				openButton.setVisible(true);
				runButton.setVisible(true);
				runButton2.setVisible(true);
				stop.setVisible(false);
				statusbar.setText("Program stoped");
				t.stop();
			}
		});
		c.add(openButton);
		c.add(runButton);
		c.add(runButton2);
		c.add(statusbar);
		c.add(stop);
		c.add(panel1);
		c.add(tExec);
		c.add(choix);
		c.add(s_dep);
	}


	public String getFichier() {
		return fichier;
	}

	public void setFichier(String fichier) {
		this.fichier = fichier;
	}


	public void showb(){
		openButton.setVisible(true);
		runButton.setVisible(true);
	}
	public void hide(){
		pb.setVisible(false);
		stop.setVisible(false);
		statusbar.setText("Terminated");
		this.showb();
	}

}
